package com.huayun.plugin.materialSetting.service;

import com.huayun.plugin.materialLibrary.dao.MaterialLibraryRepository;
import com.huayun.plugin.materialLibrary.domain.MaterialLibrary;
import com.huayun.plugin.materialSetting.dao.PartMaterialRepository;
import com.huayun.plugin.materialSetting.domain.InstanceMaterialInfo;
import com.huayun.plugin.materialSetting.domain.PartDocumentInfo;
import com.huayun.plugin.materialSetting.domain.PartMaterialInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class PartMaterialService {
    @Autowired
    private final PartMaterialRepository partMaterialRepository;
    private final MaterialLibraryRepository materialLibraryRepository;

    public PartMaterialService(PartMaterialRepository partMaterialRepository, MaterialLibraryRepository materialLibraryRepository) {
        this.partMaterialRepository = partMaterialRepository;
        this.materialLibraryRepository = materialLibraryRepository;
    }

    //获取当前零件绑定的材料信息
    public List<MaterialLibrary> getCurrentPartMaterialInfo(String partDocId){
        List<MaterialLibrary> currentPartMaterialInfo = new ArrayList<>();
        List<PartMaterialInfo> partMaterialInfos = partMaterialRepository.findByPartDocId(partDocId);
        for (PartMaterialInfo partMaterialInfo : partMaterialInfos) {
            String materialNumber = partMaterialInfo.getMaterial_number();
            List<MaterialLibrary> materialInfo = materialLibraryRepository.findByNumberContaining(materialNumber);
            currentPartMaterialInfo.addAll(materialInfo);
        }
        return currentPartMaterialInfo;
    }
    //获取实例绑定的材料信息
    public List<InstanceMaterialInfo> getInsMaterialInfoByDocInfos(List<PartDocumentInfo> partDocIdList){
        // 查询所有符合条件的 PartMaterialInfo 对象
        List<InstanceMaterialInfo> insMaterialInfoList = new ArrayList<>();
        for (PartDocumentInfo docInfo : partDocIdList) {
            InstanceMaterialInfo insMaterialInfo = new InstanceMaterialInfo();
            List<MaterialLibrary> currentInsMaterialInfo = this.getCurrentPartMaterialInfo(docInfo.getPartDocId());
            insMaterialInfo.setPartDocId(docInfo.getPartDocId());
            insMaterialInfo.setPartDocName(docInfo.getPartDocName());
            insMaterialInfo.setMaterialList(currentInsMaterialInfo);
            insMaterialInfoList.add(insMaterialInfo);
        }
        return insMaterialInfoList;
    }
    //添加材料到某零件并返回新数据
    public List<MaterialLibrary> addMaterialItemToCurrentPart(String partDocId,String partDocName,List<String> materialNumbers){
        for (String materialNumber : materialNumbers) {
            List<PartMaterialInfo> existMaterial = partMaterialRepository.findByPartDocIdAndMaterialNumber(partDocId,materialNumber);
            if (existMaterial.isEmpty()){
                PartMaterialInfo partMaterialInfo = new PartMaterialInfo();
                partMaterialInfo.setPart_id(partDocId);
                partMaterialInfo.setPart_name(partDocName);
                partMaterialInfo.setMaterial_number(materialNumber);
                partMaterialRepository.insert(partMaterialInfo);
            }
        }
        List<MaterialLibrary> currentPartMaterialInfo =  getCurrentPartMaterialInfo(partDocId);
        return currentPartMaterialInfo;
    }
    //删除某零件设置的材料并返回新数据
    public List<MaterialLibrary> deleteMaterialItemToCurrentPart(String partDocId,List<String> materialNumbers){
        for (String materialNumber : materialNumbers) {
            List<PartMaterialInfo> existMaterial = partMaterialRepository.findByPartDocIdAndMaterialNumber(partDocId,materialNumber);
            if (!existMaterial.isEmpty()){
                for (PartMaterialInfo partMaterialInfo : existMaterial) {
                    String _id = partMaterialInfo.getId();
                    partMaterialRepository.deleteById(_id);
                }
            }
        }
        List<MaterialLibrary> currentPartMaterialInfo =  getCurrentPartMaterialInfo(partDocId);
        return currentPartMaterialInfo;
    }
}
